package com.gxuwz.icefrede.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxuwz.icefrede.entity.Customer;
import com.gxuwz.icefrede.entity.Delivery;
import com.gxuwz.icefrede.entity.User;
import com.gxuwz.icefrede.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    /**
     * Get customer id by user id
     * @param session
     * @return
     */
    @RequestMapping("/get")
    public Long get(HttpSession session) {
        LambdaQueryWrapper<Customer> wrapper = new LambdaQueryWrapper<>();
        User user = (User) session.getAttribute("user");
        if (user != null && user.getRole().equals("USER")) {
            wrapper.eq(Customer::getUserId, user.getId());
            Customer customer = customerService.getOne(wrapper);
            if (customer == null) {
                return null;
            }
            return customer.getId();
        }else{
            return null;

        }
    }

    /**
     * add customer
     * @param
     * @return
     */
    @RequestMapping("/add")
    public String add(Customer customer) {
        boolean result = customerService.save(customer);
        if (result) {
            return "success";
        } else {
            return null;
        }
    }
}
